package com.yisu.kabao.manager.website.controller.baseInfo;

import com.yisu.kabao.ewf.common.enums.ResponseCode;
import com.yisu.kabao.ewf.common.web.EasyUIGrid;
import com.yisu.kabao.ewf.common.web.JsonResult;
import com.yisu.kabao.ewf.entity.GoodsType;
import com.yisu.kabao.manager.core.service.IGoodsTypeService;
import com.yisu.kabao.manager.domain.request.baseInfoPackage.GoodsCategoryRequest;
import com.yisu.kabao.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;

/**
 * author: 赵杰
 * Business Description：商品类目业务
 * createTime：2017/8/9
 */
@RequestMapping(value = "/goodsCategory")
@Controller
public class GoodsCategoryController extends BaseController {
    @Resource
    private IGoodsTypeService iGoodsTypeService;

    /**
     * author: 赵杰
     * Business Description：获取商品类目业务
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, HttpServletRequest request, GoodsCategoryRequest goodsCategoryRequest) throws Exception {
        try {
            if (goodsCategoryRequest.getKeyword() != null) {
                goodsCategoryRequest.setKeyword(URLDecoder.decode(goodsCategoryRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<GoodsType> goodsTypeEasyUIGrid = iGoodsTypeService.getGoodsTypeByPage(goodsCategoryRequest);
            model.addAttribute("GoodsTypeVos", goodsTypeEasyUIGrid.getRows());
            model.addAttribute("currentPage", goodsCategoryRequest.getPage());
            model.addAttribute("totalPages", (goodsCategoryRequest.getTotalRecords() / goodsCategoryRequest.getRows()) + 1);
            model.addAttribute("keyword", goodsCategoryRequest.getKeyword());
        } catch (Exception e) {
            logger.error("获取商品类目业务异常：" + e.getMessage());
        }
        return "/baseInfo/goodsCategory/index";
    }


    /**
     * author: 赵杰
     * Business Description：商品类目编辑页面初始
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Model model, HttpServletRequest request, GoodsCategoryRequest goodsCategoryRequest) throws Exception {
        GoodsType goodsType = new GoodsType();
        goodsType.setId(Long.valueOf(0));
        try {
            if (goodsCategoryRequest.getId() != 0) {
                goodsType = iGoodsTypeService.getGoodsType(goodsCategoryRequest.getId());
            }
            model.addAttribute("goodsTypeVo", goodsType);
        } catch (Exception e) {
            logger.error("获取商品类目业务异常：" + e.getMessage());
        }
        return "/baseInfo/goodsCategory/modify";
    }

    /**
     * author: 赵杰
     * Business Description：商品类目编辑操作
     *
     * @param goodsType 商品基本信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyAction(HttpServletRequest request, GoodsType goodsType) {
        try {
            if (iGoodsTypeService.modifyGoodsType(goodsType)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

    /**
     * author: 赵杰
     * Business Description：删除商品分类信息
     *
     * @param goodsCategoryRequest 含主键ID
     * @return 状态
     */
    @RequestMapping(value = "/deleteAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteAction(HttpServletRequest request, GoodsCategoryRequest goodsCategoryRequest) {
        try {
            if (goodsCategoryRequest.getId() == 0) {
                return new JsonResult(ResponseCode.FAILURE.code, "参数不完整！");
            }
            if (iGoodsTypeService.deleteGoodsType(goodsCategoryRequest.getId())) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }


}
